package com.example.beson.androiddemo.login.mvp.presenter;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;

import com.example.beson.androiddemo.R;
import com.example.beson.androiddemo.fragment.IosFragment;
import com.example.beson.androiddemo.fragment.JavaFragment;
import com.example.beson.androiddemo.fragment.MainFragment;
import com.example.beson.androiddemo.fragment.PhpFragment;
import com.example.beson.androiddemo.login.mvp.BasePresenter;
import com.example.beson.androiddemo.login.mvp.view.MainView;

/**
 * Created by 史国院 on 2017/4/26.
 * Desc ******
 */

public class MainPresenter implements BasePresenter{
    private MainView mainView;
    private AppCompatActivity context;

    private FragmentTabHost tabHost;
    private final Class fragment[]={
            MainFragment.class, IosFragment.class, JavaFragment.class, PhpFragment.class};
    private String title[]={"android","ios","java","php"};
    private int imageTab[]={R.drawable.android_bg,R.drawable.ios_bg,R.drawable.java_bg,R.drawable.php_bg};
    public MainPresenter(MainView mainView, AppCompatActivity context) {
        this.mainView = mainView;
        this.context = context;
    }

    @Override
    public void start() {

    }

    @Override
    public void finish() {
        if(mainView!=null){
            mainView=null;
        }
    }

    /**
     * 初始化fragmenttabhost
     */
    public void initFragment(){
        tabHost= (FragmentTabHost) context.findViewById(android.R.id.tabhost);
        tabHost.setup(context,context.getSupportFragmentManager(),R.id.realtabcontent);
        int fragmentCount=fragment.length;
        for(int i=0;i<fragmentCount;i++){
            TabHost.TabSpec tabSpec=tabHost.newTabSpec(title[i]).setIndicator(getTabItemView(i));
            tabHost.addTab(tabSpec,fragment[i],null);
        }
        tabHost.getTabWidget().setDividerDrawable(null);

    }

    private View getTabItemView(int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_main_view, null);
        ImageView iv_icon = (ImageView) view.findViewById(R.id.imageview);
        iv_icon.setImageResource(imageTab[i]);
        return view;
    }
}
